﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;
using msBsc.Cls;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 地接客资格证
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015-8-31 13:12
    public class CBizQualificationCertificate : CustBizObject
    {
        private CDmDjkQualificationCertificate m_CDmDjkQualificationCertificate = new CDmDjkQualificationCertificate();
        private readonly List<CDmDjkQualificationCertificate> _allAreaInfoList = null;
        private CDmDjkQualificationCertificate oDmDjkQualificationCertificate
        {
            get { return m_CDmDjkQualificationCertificate; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizQualificationCertificate()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkQualificationCertificate.DJK_QUALIFICATION_CERTIFICATE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["QUALIFICATION_CERTIFICATE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {

            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加资格证
        /// </summary>
        /// <param name="djackId">地接客Id</param>
        /// <param name="certificateNumber">资格证号</param>
        /// <param name="certificatePicture">资格证图片</param>
        /// <param name="name">姓名</param>
        /// <param name="gender">性别(0：女，1：男)</param>
        /// <param name="nationId">国籍Id</param>
        /// <param name="nationName">国籍名称</param>
        /// <param name="countryId">国家Id</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="cityId">城市Id</param>
        /// <param name="cityName">城市名称</param>
        /// <param name="destinationId">目的地Id</param>
        /// <param name="destinationName">目的地名称</param>
        /// <param name="birthDate">生日</param>
        /// <param name="issueDate">初次领证日期</param>
        /// <param name="quasiType">准驾类型</param>
        /// <param name="effStartDate">有效起始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <param name="certificateType">资格类别</param>
        /// <param name="publisherId">添加人</param>
        /// <returns></returns>
        ///  Author:任延华
        ///  Creation date:2015-9-9
        public string AddCertificate(string djackId, string certificateNumber, string certificatePicture, string name, string gender, string nationId, string nationName, string countryId, string countryName, string cityId, string cityName, string destinationId, string destinationName, string birthDate, string issueDate, string quasiType, string effStartDate, string endDate, string certificateType, string publisherId)
        {
            #region 数据验证
            if (string.IsNullOrEmpty(djackId) || string.IsNullOrEmpty(certificateNumber) || string.IsNullOrEmpty(certificatePicture) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(gender) || string.IsNullOrEmpty(nationId) || string.IsNullOrEmpty(nationName) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(birthDate) || string.IsNullOrEmpty(issueDate) || string.IsNullOrEmpty(quasiType) || string.IsNullOrEmpty(effStartDate) || string.IsNullOrEmpty(endDate) || string.IsNullOrEmpty(certificateType))
            {
                return "请完善资格证信息！";
            }

            //日期判断
            if (certificateNumber.Length > 30)
            {
                return "资格证号最大长度不得超过30个字符";
            }
            if (name.Length > 20)
            {
                return "姓名最多不能超过20个字符";
            }
            if (certificateType.Length > 18)
            {
                return "资格证类别最多不能超过18个字符";
            }
            if (!CommValiCollection.IsDate(birthDate))
            {
                return "出生日期格式不正确";
            }

            if (!CommValiCollection.IsDate(issueDate))
            {
                return "领证日期格式不正确";
            }

            if (!CommValiCollection.IsDate(effStartDate))
            {
                return "起效日期格式不正确";
            }

            if (!CommValiCollection.IsDate(endDate))
            {
                return "无效日期格式不正确";
            }
            int sex = 0;
            int.TryParse(gender, out sex);
            #endregion

            #region 添加证件

            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            transactionOption.Timeout = new TimeSpan(0, 10, 0);
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
            {
                DataSet ds = new CDmDjkQualificationCertificate();
                DataRow dr = ds.Tables[0].NewRow();
                dr["QUALIFICATION_CERTIFICATE_ID"] = this.DataObj.NewID();
                dr["DOCUMENT_NO"] = certificateNumber;
                dr["REAL_NAME"] = name;
                dr["GENDER"] = sex;
                dr["NATIONLITY_ID"] = nationId;
                dr["NATIONLITY_NAME"] = nationName;
                dr["COUNTRY_ID"] = countryId;
                dr["COUNTRY_NAME"] = countryName;
                dr["CITY_ID"] = cityId;
                dr["CITY_NAME"] = cityName;
                dr["AREA_ID"] = destinationId;
                dr["AREA_NAME"] = destinationName;
                dr["BIRTH_DATE"] = birthDate;
                dr["ISSUE_DATE"] = issueDate;
                dr["QUASI_DRIVING_VEHICLE"] = quasiType;
                dr["QUALIFICATION_CATEGORY"] = certificateType;
                dr["VALID_START_DATE"] = effStartDate;
                dr["VALIDITY"] = endDate;
                dr["CREATE_USERID"] = publisherId;
                dr["CREATE_DATE"] = this.dtCurrDateTime;
                dr["BELONGS_USERID"] = djackId;
                dr["IS_DELETE"] = 0;
                dr["DOCUMENT_IMAGE"] = certificatePicture;
                ds.Tables[0].Rows.Add(dr);
                this.Update(ds);
                using (CBizCustomerBaseInfo baseInfo = new CBizCustomerBaseInfo())
                {
                    string[] certie = new string[] { "IS_QUALIFICATION_CERTIFICATE" };
                    string ret = baseInfo.UpdateCertificatesStatus(certie, djackId, true);
                    if (ret != "true")
                    {
                        return "更新用户标识出错";
                    }
                }
                ts.Complete();
                return "OK";
            }
            #endregion
        }
        /// <summary>
        /// 修改资格证
        /// </summary>
        /// <param name="crtificateId">资格证Id</param>
        /// <param name="certificateNumber">资格证号</param>
        /// <param name="certificatePicture">资格证图片</param>
        /// <param name="name">姓名</param>
        /// <param name="gender">性别(0：女，1：男)</param>
        /// <param name="nationId">国籍Id</param>
        /// <param name="nationName">国籍名称</param>
        /// <param name="countryId">国家Id</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="cityId">城市Id</param>
        /// <param name="cityName">城市名称</param>
        /// <param name="destinationId">目的地Id</param>
        /// <param name="destinationName">目的地名称</param>
        /// <param name="birthDate">生日</param>
        /// <param name="issueDate">初次领证日期</param>
        /// <param name="quasiType">准驾类型</param>
        /// <param name="effStartDate">有效起始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <param name="certificateType">资格类别</param>
        /// <returns></returns>
        ///  Author:蒲崇彬
        ///  Creation date:2015-9-14
        public string UpdateCertificate(string crtificateId, string certificateNumber, string certificatePicture, string name, string gender, string nationId, string nationName, string countryId, string countryName, string cityId, string cityName, string destinationId, string destinationName, string birthDate, string issueDate, string quasiType, string effStartDate, string endDate, string certificateType)
        {
            #region 数据验证
            if (string.IsNullOrEmpty(crtificateId) || string.IsNullOrEmpty(certificateNumber) || string.IsNullOrEmpty(certificatePicture) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(gender) || string.IsNullOrEmpty(nationId) || string.IsNullOrEmpty(nationName) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(birthDate) || string.IsNullOrEmpty(issueDate) || string.IsNullOrEmpty(quasiType) || string.IsNullOrEmpty(effStartDate) || string.IsNullOrEmpty(endDate) || string.IsNullOrEmpty(certificateType))
            {
                return "请完善资格证信息！";
            }

            //日期判断
            if (certificateNumber.Length > 30)
            {
                return "资格证号最大长度不得超过30个字符";
            }
            if (name.Length > 20)
            {
                return "姓名最多不能超过20个字符";
            }
            if (certificateType.Length > 18)
            {
                return "资格证类别最多不能超过18个字符";
            }
            if (!CommValiCollection.IsDate(birthDate))
            {
                return "出生日期格式不正确";
            }

            if (!CommValiCollection.IsDate(issueDate))
            {
                return "领证日期格式不正确";
            }

            if (!CommValiCollection.IsDate(effStartDate))
            {
                return "起效日期格式不正确";
            }

            if (!CommValiCollection.IsDate(endDate))
            {
                return "无效日期格式不正确";
            }
            int sex = 0;
            int.TryParse(gender, out sex);
            #endregion

            DataSet ds = GetQualificationById(crtificateId);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return "证件信息不存在";
            }
            DataRow dr = ds.Tables[0].Rows[0];
            dr["DOCUMENT_NO"] = certificateNumber;
            dr["REAL_NAME"] = name;
            dr["GENDER"] = sex;
            dr["NATIONLITY_ID"] = nationId;
            dr["NATIONLITY_NAME"] = nationName;
            dr["COUNTRY_ID"] = countryId;
            dr["COUNTRY_NAME"] = countryName;
            dr["CITY_ID"] = cityId;
            dr["CITY_NAME"] = cityName;
            dr["AREA_ID"] = destinationId;
            dr["AREA_NAME"] = destinationName;
            dr["BIRTH_DATE"] = birthDate;
            dr["ISSUE_DATE"] = issueDate;
            dr["QUASI_DRIVING_VEHICLE"] = quasiType;
            dr["QUALIFICATION_CATEGORY"] = certificateType;
            dr["VALID_START_DATE"] = effStartDate;
            dr["VALIDITY"] = endDate;
            dr["DOCUMENT_IMAGE"] = certificatePicture;
            ds.Tables[0].TableName = this.TableName;
            this.Update(ds);

            return "OK";
        }

        /// <summary>
        /// 通过资格证 id获取
        /// </summary>
        /// <param name="crtificateId">资格证 id</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-14 9:35
        public DataSet GetQualificationById(string crtificateId)
        {
            if (string.IsNullOrEmpty(crtificateId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("crtificateId", ParameterDirection.Input, EDbType.String, 36, crtificateId));

            return this.DataObj.QueryBySQL(@"SELECT [QUALIFICATION_CERTIFICATE_ID],[DOCUMENT_NO],[REAL_NAME],[BIRTH_DATE],[GENDER],[NATIONLITY_ID],
[NATIONLITY_NAME],[COUNTRY_ID],[COUNTRY_NAME],[CITY_ID],[CITY_NAME],[AREA_ID],[AREA_NAME],[QUASI_DRIVING_VEHICLE],[QUALIFICATION_CATEGORY],[ISSUE_DATE],[VALID_START_DATE],
[VALIDITY],[BELONGS_USERID],[DOCUMENT_IMAGE] FROM [dbo].[DJK_QUALIFICATION_CERTIFICATE](NOLOCK)  WHERE [QUALIFICATION_CERTIFICATE_ID]=@crtificateId", parmCollection);
        }

        /// <summary>
        /// 通过地接客 id获取
        /// </summary>
        /// <param name="crtificateId">地接客 id</param>
        /// <returns></returns>
        /// Author:任延华
        /// Creation date:2015-9-24
        public DataSet GetQualificationDjackId(string djackId)
        {
            if (string.IsNullOrEmpty(djackId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("djackId", ParameterDirection.Input, EDbType.String, 36, djackId));

            return this.DataObj.QueryBySQL(@"SELECT [QUALIFICATION_CERTIFICATE_ID],[DOCUMENT_NO],[REAL_NAME],[BIRTH_DATE],[GENDER],[NATIONLITY_ID],
[NATIONLITY_NAME],[COUNTRY_ID],[COUNTRY_NAME],[CITY_ID],[CITY_NAME],[AREA_ID],[AREA_NAME],[QUASI_DRIVING_VEHICLE],[QUALIFICATION_CATEGORY],[ISSUE_DATE],[VALID_START_DATE],
[VALIDITY],[BELONGS_USERID],[DOCUMENT_IMAGE] FROM [dbo].[DJK_QUALIFICATION_CERTIFICATE](NOLOCK)  WHERE [BELONGS_USERID]=@djackId AND [IS_DELETE]=0", parmCollection);
        }
    }
}
